package com.sskj.mine.ui.activity;


import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.mvchelper.RefreshDataSource;
import com.sskj.lib.util.AppOuterCheckUtil;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.bean.AssetBean;
import com.sskj.mine.component.DaggerUserDataComponent;
import com.sskj.mine.presenter.AssetsActivityPresenter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import io.reactivex.Flowable;

@Route(path = RConfig.MINE_ASSETS)//资产管理
public class AssetsActivity extends BaseActivity<AssetsActivityPresenter> {
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    private SlimAdapter<AssetBean> slimAdapter;
    private MVCCoolHelper<List<AssetBean>> mvcCoolHelper;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;

    @Override
    protected int getLayoutResId() {
        return R.layout.mine_activity_assets;
    }

    @Override
    public AssetsActivityPresenter getPresenter() {
        return new AssetsActivityPresenter();
    }

    @Override
    protected void initView() {
        setTitle(App.INSTANCE.getString(R.string.mineAssetsActivity0));
        DaggerUserDataComponent.create().inject(this);
        HashMap<String, Boolean> coinMap = new HashMap<>();
        coinMap.put("USDT", true);
        coinMap.put("BTC", true);
        coinMap.put("ETH", true);
        coinMap.put("BCH", true);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            }
        });
        userViewModel.update();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setFirstDraw(false)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 2))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.transparent)));
        slimAdapter = SlimAdapter.create().register(R.layout.mine_recy_item_assets, new SlimInjector<AssetBean>() {
            @Override
            public void onInject(AssetBean data, IViewInjector injector, List payloads) {
                injector
                        .text(R.id.tvAssetType, data.getStockCode())
                        .text(R.id.tvAssetAvailable, data.getUsableFund())
                        .text(R.id.tvAssetFrozen, data.getFrostFund())
                        .visibility(R.id.tvAssetRecharge, coinMap.get(data.getStockCode()) == null ? View.INVISIBLE : View.VISIBLE)
                        .visibility(R.id.tvAssetWithdraw, coinMap.get(data.getStockCode()) == null ? View.INVISIBLE : View.VISIBLE)
                        .clicked(R.id.tvAssetRecharge, v -> {//充币
                            ARouter.getInstance().build(RConfig.MINE_RECHARGE)
                                    .withString(Constans.TYPE, data.getStockType())
                                    .withString(Constans.CODE, data.getStockCode())
                                    .withString(Constans.ID, data.getId())
                                    .navigation();
                        })
                        .clicked(R.id.tvAssetWithdraw, v -> {//提币
                            if (userData == null) {
                                return;
                            }
                            if (!AppOuterCheckUtil.checkTradePwd(AssetsActivity.this, userData)) {
                                return;
                            }
                            ARouter.getInstance().build(RConfig.MIME_WITHDRAW)
                                    .withString(Constans.CODE, data.getStockCode())
                                    .withString(Constans.USABLEFUND, data.getUsableFund())
                                    .withString(Constans.ID, data.getId())
                                    .navigation();

                        })
                        .clicked(R.id.tvAssetRecord, v -> {//帐单明细
                            ARouter.getInstance().build(RConfig.MINE_ASSERT_RECORD)
                                    .withString(Constans.CODE, data.getStockCode())
                                    .withString(Constans.ID, data.getId())
                                    .navigation();
                        });
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        RefreshDataSource<AssetBean> refreshDataSource = new RefreshDataSource<AssetBean>(coolRefreshView) {
            @Override
            public Flowable<List<AssetBean>> loadData() {
                return mPresenter.getAssets();
            }
        };
        mvcCoolHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcCoolHelper.setDataSource(refreshDataSource);
        mvcCoolHelper.setAdapter(slimAdapter);
        mvcCoolHelper.refresh();

    }

}
